﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using E5_KhuyenMaiVang.Models;

namespace E5_KhuyenMaiVang.Controllers
{
    public class HomeController : Controller
    {
        private GoldDealEntities dbGoldDeal = new GoldDealEntities();
        const int pageSize = 4;        
            
        public ActionResult Index(int page = 1)
        {
            if (Session["CityID"] == null) {
                Session["CityID"] = 0;
            }
            int SelectedCityID = (int)Session["CityID"];
            
            var listDeals = dbGoldDeal.DEAL
                .Where(d => d.StatusID > 1 && d.StatusID < 4)
                .OrderBy(p => p.DealName)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToList();
            ViewBag.CurrentPage = page;
            ViewBag.PageSize = pageSize; 
            ViewBag.TotalPages = Math.Ceiling((double)dbGoldDeal.DEAL.Where(d => d.StatusID > 1 && d.StatusID < 4).Count() / pageSize);
            if (SelectedCityID != 0) {
                var listDealsSelect = dbGoldDeal.DEAL.Where(d => d.CityID == SelectedCityID)
                .Where(d => d.StatusID > 1 && d.StatusID < 4)
                .OrderBy(p => p.DealName)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToList();
                ViewBag.TotalPages = Math.Ceiling((double)dbGoldDeal.DEAL.Where(d => d.StatusID > 1 && d.StatusID < 4 && d.CityID == SelectedCityID).Count() / pageSize);
                return View(listDealsSelect);  
            }
            return View(listDeals);            
        }

        public ActionResult PhanLoai(int ID, int page = 1)
        {
            int SelectedCityID = (int)Session["CityID"];
            
            var listDeals = dbGoldDeal.DEAL
                .Where(p => p.CategoryID == ID && p.StatusID > 1 && p.StatusID < 4)
                .OrderBy(p => p.DealName)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToList();
            ViewBag.CurrentPage = page;
            ViewBag.PageSize = pageSize;
            ViewBag.TotalPages = Math.Ceiling((double)dbGoldDeal.DEAL.Where(p => p.CategoryID == ID && p.StatusID > 1 && p.StatusID < 4).Count() / pageSize);
            if (SelectedCityID != 0)
            {
                var listDealsSelect = dbGoldDeal.DEAL.Where(d => d.CityID == SelectedCityID)
                .Where(d => d.StatusID > 1 && d.StatusID < 4)
                .Where(p => p.CategoryID == ID && p.StatusID > 1 && p.StatusID < 4)
                .OrderBy(p => p.DealName)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToList();
                ViewBag.TotalPages = Math.Ceiling((double)dbGoldDeal.DEAL.Where(d => d.StatusID > 1 && d.StatusID < 4 && d.CityID == SelectedCityID).Count() / pageSize);
                return View("Index", listDealsSelect);
            }
            return View("Index", listDeals);
        }

        [HttpPost]
        public ActionResult ChangeCity(int dff_cat1num = 0) {
            Session["CityID"] = dff_cat1num;
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult RegisterEmail(string dff_keyword, int dff_cat1num=0)
        {            
            if (dff_cat1num != 0)
            {
                bool exist = dbGoldDeal.EMAIL.Any(e => e.Email1.Equals(dff_keyword) && e.CityID == dff_cat1num);
                if (!exist)
                {
                    EMAIL email = new EMAIL();
                    email.Email1 = dff_keyword;
                    email.CityID = dff_cat1num;
                    dbGoldDeal.AddToEMAIL(email);
                    dbGoldDeal.SaveChanges();
                    return PartialView("RegisterEmailSuccessful");
                }
            }
            else
            {
                bool success = false;
                List<CITY> cities = dbGoldDeal.CITY.ToList();
                foreach (var item in cities)
                {
                    bool exist = dbGoldDeal.EMAIL.Any(e => e.Email1.Equals(dff_keyword) && e.CityID == item.CityID);
                    if (!exist)
                    {
                        EMAIL email = new EMAIL();
                        email.Email1 = dff_keyword;
                        email.CityID = item.CityID;
                        dbGoldDeal.AddToEMAIL(email);
                        success = true;
                    }
                }
                dbGoldDeal.SaveChanges();
                if(success)
                    return PartialView("RegisterEmailSuccessful");                
            }
            return RedirectToAction("Index");            
        }


    }
}
